package com.zhan.rkforum.controller;


import com.zhan.rkforum.bean.Comment;
import com.zhan.rkforum.bean.DiscussPost;
import com.zhan.rkforum.bean.Event;
import com.zhan.rkforum.controller.filter.SensitiveFilter;
import com.zhan.rkforum.event.EventProducer;
import com.zhan.rkforum.service.CommentService;
import com.zhan.rkforum.service.DiscussPostService;
import com.zhan.rkforum.util.CommunityConstant;
import com.zhan.rkforum.util.HostHolder;
import com.zhan.rkforum.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

@Controller
@RequestMapping("/comment")
public class CommentController implements CommunityConstant {

    @Autowired
    private SensitiveFilter sensitiveFilter;

    @Autowired
    private CommentService commentService;

    @Autowired
    private EventProducer eventProducer;

    @Autowired
    private DiscussPostService discussPostService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Autowired
    private HostHolder hostHolder;



    @PostMapping("/add/{postId}")
    public String addComment(@PathVariable("postId") int postId, Comment comment) throws IllegalAccessException {
        comment.setUserId(hostHolder.getUser().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        comment.setContent(sensitiveFilter.filter(comment.getContent()));
        commentService.insertComment(comment);

        Event event = new Event();
        event.setUserId(hostHolder.getUser().getId())
                .setEntityId(comment.getEntityId())
                .setEntityType(comment.getEntityType())
                .setTopic(TOPIC_COMMENT)
                .setData("postId", postId);

        if(comment.getEntityType() == ENTITY_TYPE_POST){
            DiscussPost target = discussPostService.findDiscussPostById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());
        }else if(comment.getEntityType() == ENTITY_TYPE_COMMENT){
            Comment target = commentService.findCommentById(comment.getEntityId());
            event.setEntityUserId(String.valueOf(target.getUserId()));
        }
        
        eventProducer.fireEvent(event);

        if(comment.getEntityType() == ENTITY_TYPE_POST){
            event.setTopic(TOPIC_PUBLISH)
                    .setEntityType(ENTITY_TYPE_POST)
                    .setUserId(comment.getUserId())
                    .setEntityId(postId);
            eventProducer.fireEvent(event);
            //计算贴子分数
            String postScoreKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(postScoreKey, postId);

        }

        return "redirect:/discuss/detail/" + postId;
    }
}
